package com.gopay.checkorder.service.task.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.common.constants.bank.BankOrderStatus;
import com.gopay.common.domain.bank.BankOrder;

/**
 * 对单多线程实现
 * 
 * @CheckOrderTaskThread.java
 * @author yaobo
 * @2013-3-21 上午11:53:31 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("checkOrderTaskThread")
@Scope("prototype")
public class CheckOrderTaskThread implements Runnable {
	private final static Logger logger = LoggerFactory.getLogger("CHECK_ORDER");
	 @Resource(name = "bankOrderService")
    private BankOrderService bankOrderService;
	
    private LinkedBlockingQueue<BankOrder> queue;
    private CountDownLatch latch;

    @Override
    public void run() {
        BankOrder bankOrder = queue.poll();
        if (bankOrder != null) {
            // 对单
            bankOrderService.checkOrder(bankOrder, BankOrderStatus.AUTO_CHECK_SUCCESS);
            logger.info("**********************当前队列剩余订单数："+queue.size());
        }

        latch.countDown();
    }

    public void setQueue(LinkedBlockingQueue<BankOrder> queue) {
        this.queue = queue;
    }

    public void setLatch(CountDownLatch latch) {
        this.latch = latch;
    }

    public void setBankOrderService(BankOrderService bankOrderService) {
		this.bankOrderService = bankOrderService;
	}
}
